/*
 * PanelReplayControls.java
 *
 * Created on March 22, 2008, 3:01 PM
 */
package hextd.UI;

import hextd.HexTD;
import hextd.blackBox.BBAction;
import hextd.blackBox.BBActionListener;
import hextd.blackBox.BlackBox;
import hextd.util.Context;
import java.io.BufferedReader;
import java.io.StringReader;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author  hylke
 */
public class PanelReplayControls extends javax.swing.JPanel implements BBActionListener {

    private Context context;
    private HexTD mainGame;
    private BlackBox blackBoxRecord;
    private BlackBox blackBoxPlayback;
    private boolean runTill = false;
    private boolean runTillWave = false;
    private int runTillWaveNr = 0;
    private BlackBox.ActionType runTillType;
    private static String textGo = "Go";
    private static String textStop = "Stop";

    /** Creates new form PanelReplayControls */
    public PanelReplayControls() {
	this.setVisible(false);
    }

    public void setContext(Context c, HexTD mainGame) {
	this.context = c;
	this.mainGame = mainGame;
	initComponents();
    }

    public void setBlackBox(BlackBox record, BlackBox playback) {
	if (this.blackBoxPlayback != null) {
	    this.blackBoxPlayback.removeBBActionListener(this);
	}
	this.blackBoxPlayback = playback;
	this.blackBoxRecord = record;
	this.blackBoxPlayback.addBBActionListener(this);
	this.jLabel_actionPointerPlay.setText("" + this.blackBoxPlayback.getActionPointer());
    }

    @Override
    public void ActionExecuted(BBAction action) {
	this.jLabel_actionPointerPlay.setText("" + this.blackBoxPlayback.getActionPointer());
	if (this.runTill) {
	    if (this.runTillType == action.getType()) {
		if (this.runTillWave) {
		    if (this.context.getWave() <= this.runTillWaveNr) {
			return;
		    }
		}
		this.mainGame.endMaxGameSpeed();
		if (this.jCheckBox_skipPause.isSelected()) {
		    this.mainGame.togglePause();
		}
		this.runTill = false;
		this.jButton_skipOn.setText(PanelReplayControls.textGo);
	    }
	}
    }

    @Override
    public void ActionCountChanged() {
	this.jLabel_actionCountPlay.setText("" + this.blackBoxPlayback.getActionCount());
	SpinnerNumberModel model = (SpinnerNumberModel) this.jSpinner_waveNr.getModel();
	model.setMaximum(this.blackBoxPlayback.getNextWaveActionCount());
	this.jPanel1.repaint();
	this.jLabel_actionPointerPlay.setText("" + this.blackBoxPlayback.getActionPointer());
    }

    public void skipTillEnd() {
	this.jRadioButton_end.setSelected(true);
	this.startSkip();
    }

    private void startSkip() {
	if (this.runTill) {
	    this.mainGame.endMaxGameSpeed();
	    this.runTill = false;
	    this.jButton_skipOn.setText(PanelReplayControls.textGo);
	} else {
	    this.jButton_skipOn.setText(PanelReplayControls.textStop);
	    this.runTillWave = false;
	    if (this.jRadioButton_nextWave.isSelected()) {
		this.runTillType = BlackBox.ActionType.NextWave;
		this.runTill = true;
		this.mainGame.setMaxGameSpeed();
		this.mainGame.setPause(false);
		return;
	    }
	    if (this.jRadioButton_skipWaveNr.isSelected()) {
		try {
		    this.runTillWaveNr = new Integer(this.jSpinner_waveNr.getValue().toString()).intValue();
		    this.runTillType = BlackBox.ActionType.NextWave;
		    this.runTill = true;
		    this.runTillWave = true;
		    this.mainGame.setMaxGameSpeed();
		    this.mainGame.setPause(false);
		    return;
		} catch (NumberFormatException ex) {
		    ex.printStackTrace();
		}
	    }
	    if (this.jRadioButton_end.isSelected()) {
		this.runTillType = BlackBox.ActionType.End;
		this.runTill = true;
		this.mainGame.setMaxGameSpeed();
		this.mainGame.setPause(false);
		return;
	    }
	}
	this.jButton_skipOn.setText(PanelReplayControls.textGo);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        jFrame1 = new javax.swing.JFrame();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane_output = new javax.swing.JTextPane();
        jButton_loadReplay = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton_skipOn = new javax.swing.JButton();
        jCheckBox_skipPause = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jSpinner_waveNr = new javax.swing.JSpinner();
        jRadioButton_nextWave = new javax.swing.JRadioButton();
        jRadioButton_end = new javax.swing.JRadioButton();
        jRadioButton_skipWaveNr = new javax.swing.JRadioButton();
        jButton_print = new javax.swing.JButton();
        jButton_load = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel_actionCountPlay = new javax.swing.JLabel();
        jLabel_actionPointerPlay = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton_pause = new javax.swing.JButton();
        jButton_play = new javax.swing.JButton();

        jFrame1.getContentPane().setLayout(new java.awt.GridBagLayout());

        jScrollPane2.setMinimumSize(new java.awt.Dimension(200, 200));
        jScrollPane2.setPreferredSize(new java.awt.Dimension(200, 200));
        jScrollPane2.setViewportView(jTextPane_output);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.weighty = 0.01;
        jFrame1.getContentPane().add(jScrollPane2, gridBagConstraints);

        jButton_loadReplay.setText("Load Replay");
        jButton_loadReplay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_loadReplayActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.01;
        jFrame1.getContentPane().add(jButton_loadReplay, gridBagConstraints);

        setLayout(new java.awt.GridBagLayout());

        setBackground(context.colors.getColorBackGround());
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Replay", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 11), context.colors.getColorForeGround()));
        setForeground(context.colors.getColorForeGround());
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jPanel1.setBackground(context.colors.getColorBackGround());
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Skip till", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 11), context.colors.getColorForeGround()));
        jPanel1.setForeground(context.colors.getColorForeGround());
        jButton_skipOn.setBackground(context.colors.getColorBackGround());
        jButton_skipOn.setForeground(context.colors.getColorForeGround());
        jButton_skipOn.setText(this.textGo);
        jButton_skipOn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_skipOnActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel1.add(jButton_skipOn, gridBagConstraints);

        jCheckBox_skipPause.setBackground(context.colors.getColorBackGround());
        jCheckBox_skipPause.setForeground(context.colors.getColorForeGround());
        jCheckBox_skipPause.setText("and Pause");
        jCheckBox_skipPause.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox_skipPause.setMargin(new java.awt.Insets(0, 0, 0, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jCheckBox_skipPause, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jPanel2.setBackground(context.colors.getColorBackGround());
        jPanel2.setForeground(context.colors.getColorForeGround());
        jSpinner_waveNr.setBackground(context.colors.getColorBackGround());
        jSpinner_waveNr.setForeground(context.colors.getColorForeGround());
        jSpinner_waveNr.setMinimumSize(new java.awt.Dimension(50, 20));
        jSpinner_waveNr.setPreferredSize(new java.awt.Dimension(50, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jSpinner_waveNr, gridBagConstraints);

        jRadioButton_nextWave.setBackground(context.colors.getColorBackGround());
        buttonGroup1.add(jRadioButton_nextWave);
        jRadioButton_nextWave.setForeground(context.colors.getColorForeGround());
        jRadioButton_nextWave.setSelected(true);
        jRadioButton_nextWave.setText("Next Wave");
        jRadioButton_nextWave.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton_nextWave.setMargin(new java.awt.Insets(0, 0, 0, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jRadioButton_nextWave, gridBagConstraints);

        jRadioButton_end.setBackground(context.colors.getColorBackGround());
        buttonGroup1.add(jRadioButton_end);
        jRadioButton_end.setForeground(context.colors.getColorForeGround());
        jRadioButton_end.setText("End");
        jRadioButton_end.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton_end.setMargin(new java.awt.Insets(0, 0, 0, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jRadioButton_end, gridBagConstraints);

        jRadioButton_skipWaveNr.setBackground(context.colors.getColorBackGround());
        buttonGroup1.add(jRadioButton_skipWaveNr);
        jRadioButton_skipWaveNr.setForeground(context.colors.getColorForeGround());
        jRadioButton_skipWaveNr.setText("Wave nr:");
        jRadioButton_skipWaveNr.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton_skipWaveNr.setMargin(new java.awt.Insets(0, 0, 0, 0));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        jPanel2.add(jRadioButton_skipWaveNr, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        jPanel1.add(jPanel2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(jPanel1, gridBagConstraints);

        jButton_print.setBackground(context.colors.getColorBackGround());
        jButton_print.setForeground(context.colors.getColorForeGround());
        jButton_print.setText("Print");
        jButton_print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_printActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButton_print, gridBagConstraints);

        jButton_load.setBackground(context.colors.getColorBackGround());
        jButton_load.setForeground(context.colors.getColorForeGround());
        jButton_load.setText("Load");
        jButton_load.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_loadActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButton_load, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        jPanel3.setBackground(context.colors.getColorBackGround());
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Playback", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 11), context.colors.getColorForeGround()));
        jPanel3.setForeground(context.colors.getColorForeGround());
        jLabel1.setBackground(context.colors.getColorBackGround());
        jLabel1.setForeground(context.colors.getColorForeGround());
        jLabel1.setText("Actions:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel3.add(jLabel1, gridBagConstraints);

        jLabel_actionCountPlay.setBackground(context.colors.getColorBackGround());
        jLabel_actionCountPlay.setForeground(context.colors.getColorForeGround());
        jLabel_actionCountPlay.setText("0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel3.add(jLabel_actionCountPlay, gridBagConstraints);

        jLabel_actionPointerPlay.setBackground(context.colors.getColorBackGround());
        jLabel_actionPointerPlay.setForeground(context.colors.getColorForeGround());
        jLabel_actionPointerPlay.setText("0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel3.add(jLabel_actionPointerPlay, gridBagConstraints);

        jLabel2.setBackground(context.colors.getColorBackGround());
        jLabel2.setForeground(context.colors.getColorForeGround());
        jLabel2.setText("Next:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
        jPanel3.add(jLabel2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(jPanel3, gridBagConstraints);

        jButton_pause.setBackground(context.colors.getColorBackGround());
        jButton_pause.setForeground(context.colors.getColorForeGround());
        jButton_pause.setMnemonic('P');
        jButton_pause.setText("Pause");
        jButton_pause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_pauseActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButton_pause, gridBagConstraints);

        jButton_play.setBackground(context.colors.getColorBackGround());
        jButton_play.setForeground(context.colors.getColorForeGround());
        jButton_play.setText("Go Play");
        jButton_play.setToolTipText("Leave recording centre and play from this point.");
        jButton_play.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_playActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButton_play, gridBagConstraints);

    }// </editor-fold>//GEN-END:initComponents
    private void jButton_playActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_playActionPerformed
	this.mainGame.setPause(true);
	this.context.gameState = HexTD.gameState.playing;
	this.mainGame.setUI();
    }//GEN-LAST:event_jButton_playActionPerformed

    private void jButton_pauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_pauseActionPerformed
	this.mainGame.togglePause();
    }//GEN-LAST:event_jButton_pauseActionPerformed

    private void jButton_printActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_printActionPerformed
	this.jTextPane_output.setText(this.blackBoxPlayback.printLog().toString());
	this.jFrame1.setVisible(true);
	this.jFrame1.pack();
    }//GEN-LAST:event_jButton_printActionPerformed

    private void jButton_loadReplayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_loadReplayActionPerformed
	this.jFrame1.setVisible(false);
	synchronized (this.context.gameTimeLock) {
	    this.blackBoxPlayback.clear();
	    this.blackBoxPlayback.loadLog(new BufferedReader(new StringReader(this.jTextPane_output.getText())));
	    this.blackBoxRecord.clear();
	    this.context.gameState = HexTD.gameState.replaying;
	    this.context.resetMap();
	    this.mainGame.setUI();
	}
    }//GEN-LAST:event_jButton_loadReplayActionPerformed

    private void jButton_loadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_loadActionPerformed
	this.jFrame1.pack();
	this.jFrame1.setVisible(true);
        
    }//GEN-LAST:event_jButton_loadActionPerformed

    private void jButton_skipOnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_skipOnActionPerformed
	this.startSkip();
    }//GEN-LAST:event_jButton_skipOnActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton_load;
    private javax.swing.JButton jButton_loadReplay;
    private javax.swing.JButton jButton_pause;
    private javax.swing.JButton jButton_play;
    private javax.swing.JButton jButton_print;
    private javax.swing.JButton jButton_skipOn;
    private javax.swing.JCheckBox jCheckBox_skipPause;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel_actionCountPlay;
    private javax.swing.JLabel jLabel_actionPointerPlay;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton jRadioButton_end;
    private javax.swing.JRadioButton jRadioButton_nextWave;
    private javax.swing.JRadioButton jRadioButton_skipWaveNr;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner jSpinner_waveNr;
    private javax.swing.JTextPane jTextPane_output;
    // End of variables declaration//GEN-END:variables
}
